﻿using System;
using System.Collections.ObjectModel;
using TrackIT.Common.Providers;
using TrackIT.Common.Repositories;
using TrackIT.Common.Utils;

namespace TrackIT.Common.ViewModel
{
    public class TaskViewModel : ViewModelBase
    {
        private readonly IWorkLogRepository _workLogRepository;
        private readonly IDateTimeProvider _dateTimeProvider;

        #region Properties

        public ObservableCollection<Task> Tasks { get; set; }

        private WorkEntry _currentWorkEntry;

        public WorkEntry CurrentWorkEntry
        {
            get
            {
                return _currentWorkEntry;
            }
            set
            {
                _currentWorkEntry = value;
                OnPropertyChanged("CurrentWorkEntry");
            }
        }

        private bool _enableCombo;

        public bool EnableCombo
        {
            get
            {
                return _enableCombo;
            }
            set
            {
                _enableCombo = value;
                OnPropertyChanged("EnableCombo");
            }
        }

        private string _operationLabel;

        public string OperationLabel
        {
            get
            {
                return _operationLabel;
            }
            set
            {
                _operationLabel = value;
                OnPropertyChanged("OperationLabel");
            }
        }

        private string _description;

        public string Description
        {
            get
            {
                return _description;
            }
            set
            {
                _description = value;
                OnPropertyChanged("Description");
            }
        }

        private string _timeLabel;

        public string TimeLabel
        {
            get
            {
                return _timeLabel;
            }
            set
            {
                _timeLabel = value;
                OnPropertyChanged("TimeLabel");
            }
        }

        private Task _selectedTask;

        public Task SelectedTask
        {
            get
            {
                return _selectedTask;
            }
            set
            {
                if (value != _selectedTask)
                {
                    _selectedTask = value;
                    OnPropertyChanged("SelectedTask");
                    if (_selectedTask != null)
                    {
                        Description = _selectedTask.Description;
                        CurrentWorkEntry.Task = _selectedTask.Name;
                    }
                }
            }
        }

        public string TaskText { get; set; }

        #endregion Properties

        public TaskViewModel(IWorkLogRepository workLogRepository, IDateTimeProvider dateTimeProvider)
        {
            _workLogRepository = workLogRepository;
            _dateTimeProvider = dateTimeProvider;
            Tasks = new ObservableCollection<Task>();
            _currentWorkEntry = new WorkEntry();
            _workLogRepository.Load();
        }

        public void GetTaskList()
        {
            Tasks.Clear();
            if (_workLogRepository.Tasks != null)
            {
                foreach (Task task in _workLogRepository.Tasks)
                {
                    Tasks.Add(task);
                }
                OnPropertyChanged("Tasks");
            }
        }

        public void ToggleActionAndShow()
        {
            GetTaskList();
            WorkEntryType nextAction = _workLogRepository.GetNextActionType();
            WorkEntry latest = _workLogRepository.Current;

            if (nextAction == WorkEntryType.Stop)
            {
                EnableCombo = false;
                OperationLabel = WorkEntryType.Stop.ToString();
                SelectedTask = _workLogRepository.GetTaskByName(latest.Task);
                CurrentWorkEntry.Start = latest.Start;
                CurrentWorkEntry.Stop = Util.RemoveUnecessarySecondsParts(_dateTimeProvider.Now);
                TimeSpan timeSpent = _workLogRepository.GetTimeSpentOnTask(latest.Task);
                Description = string.Format("{2}  - Time spent so far: {0} hours {1} minutes", timeSpent.Hours, timeSpent.Minutes, SelectedTask);
            }
            else
            {
                EnableCombo = true;
                OperationLabel = WorkEntryType.Start.ToString();
                Description = "";
                SelectedTask = null;
                CurrentWorkEntry.Start = Util.RemoveUnecessarySecondsParts(_dateTimeProvider.Now);
                CurrentWorkEntry.Stop = null;
            }

            TimeLabel = string.Format("at {0:HH:mm}", DateTime.Now);
        }

        public bool CommitCurrentAction()
        {
            WorkEntryType nextType = _workLogRepository.GetNextActionType();

            if (nextType == WorkEntryType.Stop && CurrentWorkEntry == null)
                return false;

            if (nextType == WorkEntryType.Start && (SelectedTask == null && string.IsNullOrWhiteSpace(TaskText)))
                return false;

            if (nextType == WorkEntryType.Stop)
            {
                _workLogRepository.Stop();
            }
            else
            {
                _workLogRepository.Start(TaskText ?? SelectedTask.Name);
            }
            _workLogRepository.Save();

            return true;
        }
    }
}